/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdminLeftPanel.java
 *
 * Created on Apr 27, 2012, 3:56:34 PM
 */
package GUI;

import GUI.Admin.AdminBooking;
import GUI.Admin.AdminFilm;
import GUI.Admin.AdminFilmSchedule;
import GUI.Admin.AdminFilmType;
import GUI.Admin.AdminRoom;
import GUI.Admin.AdminSeat;
import GUI.Admin.AdminShowTimes;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.ActionMap;
import javax.swing.JComponent;
import main.MainFormApp;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.Task;
import org.jdesktop.application.Task.BlockingScope;
import utils.BusyIndicator;
import utils.BusyIndicatorInputBlocker;

/**
 *
 * @author Hnmtuan
 */
public class AdminLeftPanel extends javax.swing.JPanel {

    SingleFrameApplication localApplication;
    BusyIndicator localBusy;
    public static int WIDTH_SIZE = 230;
    public static int HEIGHT_SIZE = 600;
    public static final int PREFERRED_GAP = 5;
    public static final int MAX_GAP = 10;

    static {
        HEIGHT_SIZE = MainFormApp.HEIGHT_SIZE - (GlobalTopPanel.HEIGHT_SIZE + GlobalFooterPanel.HEIGHT_SIZE);
    }

    /** Creates new form AdminLeftPanel */
    public AdminLeftPanel(SingleFrameApplication appContext, BusyIndicator busy) {
        localApplication = appContext;
        localBusy = busy;
        initComponents();
        setPreferredSize(new Dimension(WIDTH_SIZE, HEIGHT_SIZE));
        initActionComponents();
    }

    protected final void initActionComponents() {
        ActionMap actMap = localApplication.getContext().getActionMap(AdminLeftPanel.class, this);
        jbtnBooking.setAction(actMap.get("changeBooking"));
        jbtnFilmManage.setAction(actMap.get("changeFilmManage"));
        jbtnFilmTypeManage.setAction(actMap.get("changeFilmTypeManage"));
        jbtnRoomManage.setAction(actMap.get("changeRoomManage"));
        jbtnSeatManage.setAction(actMap.get("changeSeatManage"));
        jbtnShowTimeManage.setAction(actMap.get("changeShowTimeManage"));
        jbtnScheduleManage.setAction(actMap.get("changeScheduleManage"));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSeparator1 = new javax.swing.JSeparator();
        jbtnFilmManage = new javax.swing.JButton();
        jbtnFilmTypeManage = new javax.swing.JButton();
        jbtnRoomManage = new javax.swing.JButton();
        jbtnSeatManage = new javax.swing.JButton();
        jbtnShowTimeManage = new javax.swing.JButton();
        jbtnScheduleManage = new javax.swing.JButton();
        jbtnBooking = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        jSeparator1.setName("jSeparator1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(AdminLeftPanel.class);
        jbtnFilmManage.setIcon(resourceMap.getIcon("jbtnFilmManage.icon")); // NOI18N
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("GUI/resources/AdminLeftPanel"); // NOI18N
        jbtnFilmManage.setText(bundle.getString("jbtnFilmManage.title")); // NOI18N
        jbtnFilmManage.setName("jbtnFilmManage"); // NOI18N

        jbtnFilmTypeManage.setIcon(resourceMap.getIcon("jbtnFilmTypeManage.icon")); // NOI18N
        jbtnFilmTypeManage.setText(bundle.getString("jbtnFilmTypeManage.title")); // NOI18N
        jbtnFilmTypeManage.setName("jbtnFilmTypeManage"); // NOI18N

        jbtnRoomManage.setIcon(resourceMap.getIcon("jbtnRoomManage.icon")); // NOI18N
        jbtnRoomManage.setText(bundle.getString("jbtnRoomManage.title")); // NOI18N
        jbtnRoomManage.setName("jbtnRoomManage"); // NOI18N

        jbtnSeatManage.setIcon(resourceMap.getIcon("jbtnImageManage.icon")); // NOI18N
        jbtnSeatManage.setText(bundle.getString("jbtnSeatManage.title")); // NOI18N
        jbtnSeatManage.setName("jbtnSeatManage"); // NOI18N

        jbtnShowTimeManage.setIcon(resourceMap.getIcon("jbtnShowTimeManage.icon")); // NOI18N
        jbtnShowTimeManage.setText(bundle.getString("jbtnShowTimeManage.title")); // NOI18N
        jbtnShowTimeManage.setName("jbtnShowTimeManage"); // NOI18N

        jbtnScheduleManage.setIcon(resourceMap.getIcon("jbtnFilmScheduleManage.icon")); // NOI18N
        jbtnScheduleManage.setText(bundle.getString("jbtnFilmScheduleManage.title")); // NOI18N
        jbtnScheduleManage.setName("jbtnScheduleManage"); // NOI18N

        jbtnBooking.setIcon(resourceMap.getIcon("jbtnBooking.icon")); // NOI18N
        jbtnBooking.setText(bundle.getString("jbtnProfileManage.title")); // NOI18N
        jbtnBooking.setName("jbtnBooking"); // NOI18N

        jSeparator2.setName("jSeparator2"); // NOI18N

        jLabel1.setText(bundle.getString("jlbProfile.title")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setForeground(resourceMap.getColor("jlbLogoff.foreground")); // NOI18N
        jLabel2.setText(bundle.getString("jlbLogoff.title")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jbtnFilmTypeManage, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbtnFilmManage, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbtnBooking, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE))
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jbtnSeatManage, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbtnRoomManage, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
                        .addComponent(jbtnShowTimeManage, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbtnScheduleManage, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(192, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addGap(8, 8, 8)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jbtnBooking, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jbtnFilmManage, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jbtnFilmTypeManage, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jbtnRoomManage, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jbtnSeatManage, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jbtnShowTimeManage, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jbtnScheduleManage, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JButton jbtnBooking;
    private javax.swing.JButton jbtnFilmManage;
    private javax.swing.JButton jbtnFilmTypeManage;
    private javax.swing.JButton jbtnRoomManage;
    private javax.swing.JButton jbtnScheduleManage;
    private javax.swing.JButton jbtnSeatManage;
    private javax.swing.JButton jbtnShowTimeManage;
    // End of variables declaration//GEN-END:variables

    @Action(block = BlockingScope.APPLICATION)
    public Task changeBooking() {
        Task task = new ChangePanelTask(localApplication, this, MainForm.ORDER_CHILDS.FRM_BOOKING);
        task.setInputBlocker(new BusyIndicatorInputBlocker(task, localBusy));
        return task;
    }

    @Action(block = BlockingScope.APPLICATION)
    public Task changeFilmManage() {
        Task task = new ChangePanelTask(this.localApplication, this, MainForm.ORDER_CHILDS.FRM_FILM);
        task.setInputBlocker(new BusyIndicatorInputBlocker(task, this.localBusy));
        return task;
    }

    @Action(block = BlockingScope.APPLICATION)
    public Task changeFilmTypeManage() {
        Task task = new ChangePanelTask(this.localApplication, this, MainForm.ORDER_CHILDS.FRM_FILMTYPE);
        task.setInputBlocker(new BusyIndicatorInputBlocker(task, this.localBusy));
        return task;
    }

    @Action(block = BlockingScope.APPLICATION)
    public Task changeRoomManage() {
        Task task = new ChangePanelTask(this.localApplication, this, MainForm.ORDER_CHILDS.FRM_ROOM);
        task.setInputBlocker(new BusyIndicatorInputBlocker(task, this.localBusy));
        return task;
    }

    @Action(block = BlockingScope.APPLICATION)
    public Task changeSeatManage() {
        Task task = new ChangePanelTask(this.localApplication, this, MainForm.ORDER_CHILDS.FRM_SEAT);
        task.setInputBlocker(new BusyIndicatorInputBlocker(task, this.localBusy));
        return task;
    }

    @Action(block = BlockingScope.APPLICATION)
    public Task changeShowTimeManage() {
        Task task = new ChangePanelTask(this.localApplication, this, MainForm.ORDER_CHILDS.FRM_SHOWTIMES);
        task.setInputBlocker(new BusyIndicatorInputBlocker(task, this.localBusy));
        return task;
    }

    @Action(block = BlockingScope.APPLICATION)
    public Task changeScheduleManage() {
        Task task = new ChangePanelTask(this.localApplication, this, MainForm.ORDER_CHILDS.FRM_FILMSCHEDULE);
        task.setInputBlocker(new BusyIndicatorInputBlocker(task, this.localBusy));
        return task;
    }

    private class ChangePanelTask extends Task<Void, Void> {

        private JComponent ownedComponent;
        private int orderMenu = MainForm.ORDER_CHILDS.FRM_DEFAULT;
        private final String ERROR_NOT_FOUND_CENTER_COMPONENT = "Error: Center component not found !";
        private final String STATUS_LOADING_FORM = "Loading Form ......";
        private final String STATUS_LOADING_FORM_DONE = "Loading Form Done !";
        private final String STATUS_LOADING_FORM_CANCEL = "Loading Form Cancel !";

        public ChangePanelTask(SingleFrameApplication appContext, JComponent revoikedPanel, int voikedPanel) {
            super(appContext);
            ownedComponent = revoikedPanel;
            orderMenu = voikedPanel;
        }

        @Override
        protected Void doInBackground() throws Exception {
            Container parent = ownedComponent.getParent();

            if (parent instanceof MainForm) {
                setMessage(STATUS_LOADING_FORM);
                MainForm mainForm = (MainForm) parent;
                // clear component at center of main form
                Object cachedLayout = mainForm.getLayout();
                Thread.sleep(150L);
                setProgress(1, 0, 3);
                if (cachedLayout instanceof BorderLayout) {
                    BorderLayout mainLayout = (BorderLayout) cachedLayout;
                    mainLayout.getLayoutComponent(BorderLayout.CENTER).setVisible(false);
                    JComponent cachedPanel = null;
                    switch (orderMenu) {
                        case MainForm.ORDER_CHILDS.FRM_BOOKING:
                            cachedPanel = new AdminBooking(localApplication, localBusy);
                            break;
                        case MainForm.ORDER_CHILDS.FRM_FILM:
                            cachedPanel = new AdminFilm(localApplication, localBusy);
                            break;
                        case MainForm.ORDER_CHILDS.FRM_FILMTYPE:
                            cachedPanel = new AdminFilmType(localApplication, localBusy);
                            break;
                        case MainForm.ORDER_CHILDS.FRM_ROOM:
                            cachedPanel = new AdminRoom(localApplication, localBusy);
                            break;
                        case MainForm.ORDER_CHILDS.FRM_SEAT:
                            cachedPanel = new AdminSeat(localApplication, localBusy);
                            break;
                        case MainForm.ORDER_CHILDS.FRM_SHOWTIMES:
                            cachedPanel = new AdminShowTimes(localApplication, localBusy);
                            break;
                        case MainForm.ORDER_CHILDS.FRM_FILMSCHEDULE:
                            cachedPanel = new AdminFilmSchedule(localApplication, localBusy);
                            break;
                        default:
                            break;
                    }
                    if (cachedPanel == null) {
                        throw new Exception(ERROR_NOT_FOUND_CENTER_COMPONENT);
                    }
                    cachedPanel.setVisible(true);
                    mainForm.add(cachedPanel, BorderLayout.CENTER);
                    //localApplication.getMainFrame().pack();
                    Thread.sleep(150L);
                    setProgress(2, 0, 3);
                }
            }
            Thread.sleep(150L);
            return null;
        }

        @Override
        protected void failed(Throwable thrwbl) {
            setMessage(thrwbl.getMessage());
        }

        @Override
        protected void succeeded(Void t) {
            setMessage(STATUS_LOADING_FORM_DONE);            
            setProgress(3, 0, 3);
        }

        @Override
        protected void cancelled() {
            setMessage(STATUS_LOADING_FORM_CANCEL);
        }
    }
}
